#include "cpu/exec/template-start.h"

#define instr ret

make_helper(concat(ret_n_,SUFFIX)){
    cpu.eip = MEM_R(reg_l(R_SP), 2);
    if(DATA_BYTE == 2)
        cpu.eip = cpu.eip & 0xffff;
    reg_l(R_SP) += DATA_BYTE;

    print_asm("ret");
    return 1;
}

make_helper(concat(ret_i_,SUFFIX)){
    cpu.eip = MEM_R(REG(R_ESP), 2);
    if(DATA_BYTE == 2) 
        cpu.eip = cpu.eip & 0xffff;
    REG(R_ESP) += DATA_BYTE;
    
    int val = instr_fetch(eip + 1, 2);
    int i = 0;
    for(;i < val;i += DATA_BYTE)
        MEM_W(REG(R_ESP) + i, 0, 2);
    REG(R_ESP) += val;

    print_asm("ret $0x%x",val);
    return 1;
}

#include "cpu/exec/template-end.h"